{extend name="public/common"}

{block name="style"}
<title>物资采购审批（内购）</title>
<link rel="stylesheet" href="/home/css/approval/add.css?v={$Think.config.version}">
{/block}

{block name="body"}
<div id="app" v-cloak>
    <div class="type-title">物资采购审批（内购）</div>
    <div class="form" id="purchase">
        <ul>
            <li>
                <p>类型：</p>
                <div class="box">物资采购(内购)</div>
            </li>
            <li>
                <p>申请人：</p>
                <div class="box">{{user.name}}</div>
            </li>
            <li>
                <p>申请标题：</p>
                <input type="text" v-model="title" placeholder="请输入申请标题" :maxlength="50">
            </li>
            <li>
                <p>申请部门：</p>
                <div class="box">{{user.department_name}}</div>
            </li>
            <li>
                <p>申请时间：</p>
                <div class="box">{{nowTime.getTime() / 1000 | timeToString('ymdhi')}}</div>
            </li>
            <li>
                <p>审批人：</p>
                <div @click="toggleApprover" :class="approver.nowType.userid == 0 ? 'special box' : 'box'">{{approver.nowType.username}}</div>
                <mt-popup  position="bottom"  v-model="approver.popupVisible" class="mint-popup">
                    <div class="picker-toolbar">
                        <span class="mint-datetime-action mint-datetime-cancel" @click="toggleApprover">取消</span>
                        <span class="mint-datetime-action mint-datetime-confirm" @click="approverConfirm">确定</span>
                    </div>
                    <mt-picker :slots="approver.slots" value-key="username" @change="selectApprover"></mt-picker>
                </mt-popup>
            </li>
        </ul>
        <div class="purchase-content">
            <p>申请内容<i>（最多可添加5个）</i>：</p>
            <div v-for="item,key in list" class="purchase-list">
                <div class="title">
                    <p>序列 {{key+1}}</p>
                    <span v-if="key > 0" @click="delContent(key)">删除</span>
                </div>
                <ul>
                    <li>
                        <p>名称：</p>
                        <input type="text" v-model="item.name" placeholder="请输入名称">
                        <i class="fa fa-angle-right"></i>
                    </li>
                    <li>
                        <p>规格型号：</p>
                        <input type="text" v-model="item.model" placeholder="请输入规格型号">
                        <i class="fa fa-angle-right"></i>
                    </li>
                    <li>
                        <p>单位：</p>
                        <input type="text" v-model="item.company" placeholder="请输入单位">
                        <i class="fa fa-angle-right"></i>
                    </li>
                    <li>
                        <p>数量：</p>
                        <input type="text" v-model="item.number" placeholder="请输入数量">
                        <i class="fa fa-angle-right"></i>
                    </li>
                    <li>
                        <p>要求到货时间：</p>
                        <div @click="chooseStartTime(key)" v-if="typeof item.arrival_time === 'number'" class="box">{{item.arrival_time | timeToString('ymdhi')}}
                            <i class="fa fa-angle-right"></i>
                        </div>
                        <div @click="chooseStartTime(key)" v-else class="special box">请选择要求到货时间
                            <i class="fa fa-angle-right"></i>
                        </div>
                    </li>
                    <li>
                        <p>备注：</p>
                        <input type="text" v-model="item.content" placeholder="请输入备注内容">
                        <i class="fa fa-angle-right"></i>
                    </li>
                </ul>
            </div>
            <div class="add-content" @click="addContent">添加序列</div>
            <mt-datetime-picker
                    v-model="start_time"
                    type="datetime"
                    ref="timePicker"
                    year-format="{value} 年"
                    month-format="{value} 月"
                    date-format="{value} 日"
                    hour-format="{value} 时"
                    minute-format="{value} 分"
                    :start-date="nowTime"
                    @confirm="startTimeConfirm">
            </mt-datetime-picker>
        </div>
    </div>
    <div class="foot">
        <div class="cancel" @click="cancel">取消</div>
        <div class="confirm" @click="subimtAjax">确定</div>
    </div>
</div>
{/block}

{block name="script"}
<script>
    console.log({$apply_user})
    var vw = new Vue({
        el: "#app",
        data: {
            type: 3,
            user: {$apply_user},
            start_time: new Date(),
            nowTime: new Date(),
            approver: {
                slots: [
                    {
                        values: {$approval_user},
                        defaultIndex: 1
                    }
                ],
                nowType: {
                    username: '请选择审批人',
                    userid: 0
                },
                onChangeType: {
                    username: '请选择审批人',
                    userid: 0
                },
                popupVisible: false
            },
            remark: '',
            list: [{
                name:'',
                model:'',
                company:'',
                number:'',
                arrival_time:'',
                content:''
            }],
            key:0,
            id:0,
            approvalUser: [],
            clickLoading: true,
            title: ''
        },
        created() {
            if(window.location.href.indexOf('id') > -1) {
                var hrefs = window.location.href.split("/"),
                    ids = hrefs[hrefs.length - 1].split("."),
                    _this = this;
                _this.id = ids[0];
                _this.$indicator.open({
                    text: '正在获取中',
                    spinnerType: 'triple-bounce'
                });
                _this.$http.post('{:Url("Approval/getDetail")}',{
                    'id': _this.id,
                    'class': _this.type
                },{emulateJSON:true}).then(function (res) {
                    var data = res.body;
                    _this.$indicator.close();
                    console.log(data)
                    if(data.code == 1){
                        _this.title = data.data.title;
                        _this.department = data.data.to_company;
                        _this.remark = data.data.content;
                        Object.assign(_this.approvalUser,data.data.approval_user);
                        _this.approver.nowType.userid = data.data.approval_user[0].approval_user;
                        _this.approver.nowType.username = data.data.approval_user[0].username;
                        _this.list = data.data.purchase_content;
                    }else{
                        _this.$toast({
                            message: data.msg,
                            duration: 1500
                        });
                    }
                }, function (res) {
                    _this.$indicator.close();
                    _this.$toast({
                        message: "获取失败，请重试！",
                        duration: 1500
                    });
                });
            }

        },
        mounted(){
            var _this = this;
            _this.$nextTick(function() {
                setTimeout(function() {
                    _this.approver.slots[0].defaultIndex = 0;
                }, 100);
            });
        },
        methods: {
            //审批人
            toggleApprover(){
                var _this = this;
                _this.approver.popupVisible = !_this.approver.popupVisible;
            },
            approverConfirm(){
                var _this = this;
                if(_this.approver.nowType != _this.approver.onChangeType){
                    _this.approver.nowType = _this.approver.onChangeType;
                    if(_this.id != 0) {
                        _this.approvalUser[0].username = _this.approver.nowType.username;
                        _this.approvalUser[0].approval_user = _this.approver.nowType.userid;
                    }
                }
                _this.approver.popupVisible = !_this.approver.popupVisible;
            },
            selectApprover(p,v){
                var _this = this;
                if(v[0]){
                    _this.approver.onChangeType = v[0];
                }
            },
            chooseStartTime(key) {
                if(this.key != key) {
                    this.start_time = new Date();
                    this.key = key;
                }
                this.$refs.timePicker.open();
            },
            startTimeConfirm(val) {
                this.start_time = val;
                this.list[this.key].arrival_time = new Date(this.start_time).getTime() / 1000;
            },
            addContent() {
                var obj = {
                    name:'',
                    model:'',
                    company:'',
                    number:'',
                    arrival_time:'请选择要求到货时间',
                    content:''
                };
                this.list.push(obj);
            },
            delContent(key) {
                this.list.splice(key,1);
            },
            cancel(){
                window.history.go(-1)
            },
            subimtAjax() {
                var _this =this;
                if(!_this.clickLoading) {
                    return false;
                }
                _this.$messagebox.confirm('是否确认提交','在线审批').then(function () {
                    if(!_this.title) {
                        _this.$toast({
                            message:"请输入申请标题",
                            duration:1500
                        });
                        return false;
                    }
                    if(_this.to_company == ''){
                        _this.$toast({
                            message:"请输入发往公司",
                            duration:1500
                        });
                        return false;
                    }
                    if(_this.approver.nowType.userid == 0){
                        _this.$toast({
                            message:"请选择审批人",
                            duration:1500
                        });
                        return false;
                    }
                    for(var i in _this.list) {
                        if(!_this.list[i].name || !_this.list[i].model || !_this.list[i].company || !_this.list[i].number || !_this.list[i].arrival_time){
                            _this.$toast({
                                message:"请加添加的序列内容填写完整",
                                duration:1500
                            });
                            return false;
                        }
                    }
                    _this.clickLoading = false;
                    _this.$indicator.open({
                        text: '正在发布中',
                        spinnerType: 'triple-bounce'
                    });
                    var data = {
                        'class':_this.type,
                        'apply_department':_this.user.department,
                        'apply_user':_this.user.userid,
                        'apply_time':new Date().getTime() / 1000,
                        'data':_this.list,
                        'title': _this.title
                    };
                    if(_this.id != 0) {
                        var url = '{:Url("Approval/edit")}';
                        data.id = _this.id;
                        var obj = {
                            'content': data,
                            'approval_user': _this.approvalUser
                        }
                    } else {
                        var url = '{:Url("Approval/purchase")}',
                            obj = data;
                        obj.approval_user = _this.approver.nowType.userid;
                    }
                    _this.$http.post(url,obj,{emulateJSON:true}).then(function (res) {
                        var data = res.body;
                        console.log(data)
                        _this.$indicator.close();
                        if(data.code == 1){
                            _this.$toast({message:'提交成功',iconClass: 'icon', duration:1500});
                            window.sessionStorage.setItem('approval_type','2');
                            setTimeout(function () {
                                if(_this.id != 0) {
                                    window.location.replace('/home/approval/index.html');
                                    window.sessionStorage.setItem('approval_type','2');
                                } else {
                                    window.history.go(-1);
                                }
                            },1000);
                        }else{
                            _this.clickLoading = true;
                            _this.$toast({
                                message: data.msg,
                                duration: 1500
                            });
                        }
                    }, function (res) {
                        _this.$indicator.close();
                        _this.$toast({
                            message: "提交失败，请重试！",
                            duration: 1500
                        });
                    });
                });

            }
        }
    })
</script>
{/block}
